package com.szholly.pro.watergas_server.ps.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.ITable;
import com.szholly.data.general.ITableFactory;
import com.szholly.data.general.RowBase;
import com.szholly.plug.form.excel.utils.StringUtil;
import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.utils.spring.SpringBeanFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 数据有效性统计服务
 * @author WINSON
 * @since 2015-12-17
 */
@Service
public class DataValidateService { 
	
	private static final Logger LOGGER = LoggerFactory.getLogger(DataValidateService.class);
	
	@Autowired
	DomainService domainService;
	
	@Autowired
	OnlineMonitorService onlineMonitorService;
	
	@Autowired
	WaterSampleService waterSampleService;
	
	@Autowired
	StandardVerificationService standardVerificationService;
	
	@Autowired
	StandardSubstanceService standardSubstanceService;
	
	@Autowired
	ExceedComparisonService exceedComparisonService;
	
	@Autowired
	FailureRepairService failureRepairService;
	
	@Autowired
	MaterialReplaceService materialReplaceService;
	
	@Autowired
	ArtificialMonitorService artificialMonitorService;
	
	@Autowired
	DailyInspectionService dailyInspectionService;
	
	@Autowired
	PeroidMaintainanceService peroidMaintainanceService;
	/**
	 * 获取时间参数列表
	 * @param params 参数，可以包含JOB_ID, PARAM_ID
	 * @return
	 */
	public List<Map<String, String>> getParamJobList(Map<String,String> params){
		List<GlobalParameter> listParams = new ArrayList<>();
		String strSQL = " select t.*  from OPERATION_PARM_JOB t where 1=1";
		
		if(params!=null && params.containsKey("JOB_ID")){
			listParams.add(new GlobalParameter("JOB_ID",params.get("JOB_ID")));
			strSQL += " and t.JOB_ID=@JOB_ID";
		}
		if(params!=null && params.containsKey("PARAM_ID")){
			listParams.add(new GlobalParameter("PARAM_ID",params.get("PARAM_ID")));
			strSQL += " and t.PARM_ID=@PARAM_ID";
		}
		
		strSQL	+= " order by t.JOB_ID ";
		
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		List<RowBase> rcdList = DataUtil.getSelectRows(strSQL, listParams, pTable);
		if (rcdList.size()<1)
			return null;
		
		List<Map<String, String>> JobParamList = new ArrayList<Map<String, String>>();
		for (int i=0; i<rcdList.size(); i++){
			Map<String, String> param = new HashMap<String, String>();
			param.put("JOB_ID", rcdList.get(i).getSafeString("JOB_ID"));
			param.put("JOB_NAME", rcdList.get(i).getSafeString("NAME"));
			param.put("JOB_CYCLE", rcdList.get(i).getSafeString("DEFAULT_CYCLE"));
			param.put("JOB_CYCLE_DESC", rcdList.get(i).getSafeString("DEFAULT_CYCLE_DESC"));
			param.put("JOB_TOLERANCE", rcdList.get(i).getSafeString("DEFAULT_TOLERANCE"));
			param.put("JOB_TOLERANCE_DESC", rcdList.get(i).getSafeString("DEFAULT_TOLERANCE_DESC"));
			param.put("PARAM_ID", rcdList.get(i).getSafeString("PARM_ID"));
			param.put("PARAM_NAME", rcdList.get(i).getSafeString("PARM_ID_DESC"));
			JobParamList.add(param);
		}
		
		return JobParamList;
	}
	
	/**
	 * 获取所有事件的有效性
	 * @param params 包含站点ID， 起始时间
	 * 		
	 */
	public List<Map<String, String>> queryAllJobParamValidationList(Map<String,String> params) {
		
		List<Map<String, String>> jobParamList = getParamJobList(params);
		
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
		
		for (int i = 0; i < jobParamList.size(); i++) {
			params.put("JOB_ID", jobParamList.get(i).get("JOB_ID"));
			params.put("JOB_NAME", jobParamList.get(i).get("JOB_NAME"));
			params.put("JOB_CYCLE", jobParamList.get(i).get("JOB_CYCLE"));
			params.put("JOB_CYCLE_DESC", jobParamList.get(i).get("JOB_CYCLE_DESC"));
			params.put("JOB_TOLERANCE", jobParamList.get(i).get("JOB_TOLERANCE"));
			params.put("JOB_TOLERANCE_DESC", jobParamList.get(i).get("JOB_TOLERANCE_DESC"));
			params.put("PARAM_ID", jobParamList.get(i).get("PARAM_ID"));
			params.put("PARAM_NAME", jobParamList.get(i).get("PARAM_NAME"));
			
			Map<String, String> jobParamValidation = queryOneJobParamValidationList(params);
			
			if (jobParamValidation == null)
				continue;
			
			result.add(jobParamValidation);
		}

		return result;
	}
	
	/**
	 * 获取单个事件有效性
	 * 
	 * @param params
	 *            - { STATION_ID:站点编号, PARAM_ID:污染物参数（如参数无关事件则为空） JOB_ID:事件ID,
	 *            START_TIME:开始时间, END_TIME:结束时间 }
	 * @return { JOB_ID:事件编号, </br>
	 *         JOB_NAME:事件名称, </br>
	 *         JOB_CYCLE:事件周期, </br>
	 *         JOB_CYCLE_DESC:周期描述, </br>
	 *         JOB_TOLERANCE:事件容限, </br>
	 *         JOB_TOLERANCE_DESC:容限描述 , </br>
	 *         JOB_VALIDATION:事件有效性, </br>
	 *         JOB_VALIDATION_DESC:事件有效性描述, </br>
	 *         JOB_TASK_TOTALNUM: 计划任务总数, </br>
	 *         JOB_TASK_COMPLETENUM: 完成任务数, </br>
	 *         JOB_TASK_TIMELYNUM: 及时完成任务数, </br>
	 *         JOB_TASK_PASSNUM:合格任务数 ,</br>
	 *         }
	 */
	public Map<String, String> queryOneJobParamValidationList(Map<String, String> params) {
		if (params == null)
			return null;

		String strJOB_ID = params.get("JOB_ID");
		if (StringUtil.IsNullOrEmpty(strJOB_ID))
			return null;

		Map<String, String> jobValidation = null;
		
		switch (strJOB_ID) {
		case "0f9faa25-89e4-4ad6-869f-aa1beb70f7bc": 
			jobValidation = getSampleComparisonJobValidation(params);
			break;
		case "21c74765-685e-4a89-a625-0fa8f3668fc5": 
			jobValidation = getStandardVerificationJobValidation(params);
			break;
		case "5385ab20-26ee-4fd0-a419-1cd9174a9921": 
			jobValidation = getStandardSubstanceJobValidation(params);
			break;
		case "2bf6c8b1-12c3-496c-8e1b-96b52157e12c": 
			jobValidation = getExceedComparisonJobValidation(params);
			break;
		case "5ed321b7-99ca-4f0b-b81f-008475d0174a": 
			jobValidation = getFailureRepairJobValidation(params);
			break;
		case "21c7cb0d-9ea5-46d1-b150-c04151386678": 
			jobValidation = getArtificialMonitorJobValidation(params);
			break;
		case "fa3ecf1a-4820-4f9f-b7b7-2553584ee7ba": 
			jobValidation = getDailyInspectionJobValidation(params);
			break;
		case "0bddcd82-73ff-4ee7-85af-491108b7f374": 
			jobValidation = getPeriodicalMaintainanceJobValidation(params);
			break;
		case "167b2c94-4c13-45fd-b796-8c0e4bf3f6b9": 
			jobValidation = getMaterialReplaceJobValidation(params);
			break;
		default: 
			return null;
		}

		return jobValidation;
	}
	
	/****************************************** 水样比对 **********************************************/
	/**
	 * 获取水样比对事件有效性
	 * 
	 * @param resqMap
	 * @return { JOB_ID:事件编号, </br>
	 *         JOB_NAME:事件名称, </br>
	 *         JOB_CYCLE:事件周期, </br>
	 *         JOB_CYCLE_DESC:周期描述, </br>
	 *         JOB_TOLERANCE:事件容限, </br>
	 *         JOB_TOLERANCE_DESC:容限描述 , </br>
	 *         JOB_VALIDATION:事件有效性, </br>
	 *         JOB_VALIDATION_DESC:事件有效性描述, </br>
	 *         JOB_TASK_TOTALNUM: 计划任务总数, </br>
	 *         JOB_TASK_COMPLETENUM: 完成任务数, </br>
	 *         JOB_TASK_TIMELYNUM: 及时完成任务数, </br>
	 *         JOB_TASK_PASSNUM:合格任务数 ,</br>
	 *         }
	 */
	public Map<String, String> getSampleComparisonJobValidation(Map<String, String> resqMap) {
		if (resqMap == null)
			return null;

		List<Map<String, String>> taskValidationList = getSampleComparisonTaskValidationList(resqMap);
		if (taskValidationList == null || taskValidationList.size() < 1)
			return null;

		int nTotal = 0; // 总任务数
		int nComplete = 0; // 已完成任务数
		int nTimely = 0; // 及时完成任务数
		int nPass = 0; // 合格任务数
		boolean isJobValidation = true;

		nTotal = taskValidationList.size();
		for (int i = 0; i < taskValidationList.size(); i++) {
			Map<String, String> taskValidation = taskValidationList.get(i);
			String strTASK_STATUS = taskValidation.get("TASK_STATUS");
			String strTASK_PASS = taskValidation.get("TASK_PASS");
			if (strTASK_STATUS.equals("1") || strTASK_STATUS.equals("3")) // 按时完成+超时完成
				nComplete++;

			if (strTASK_STATUS.equals("1")) // 按时完成
				nTimely++;

			if (strTASK_PASS.equals("1")) // 合格
				nPass++;
		}

		// 有效性判断
		if ((nPass /(double)nTotal >= 0.75) && (nTimely /(double)nTotal >= 0.75) && (nComplete /(double)nTotal >= 0.9)) {
			isJobValidation = true;
		} else {
			isJobValidation = false;
		}

		Map<String, String> jobValidation = cloneHashMap(resqMap);
		jobValidation.put("JOB_TASK_TOTALNUM", Integer.toString(nTotal));
		jobValidation.put("JOB_TASK_COMPLETENUM", Integer.toString(nComplete));
		jobValidation.put("JOB_TASK_TIMELYNUM", Integer.toString(nTimely));
		jobValidation.put("JOB_TASK_PASSNUM", Integer.toString(nPass));
		jobValidation.put("JOB_VALIDATION", isJobValidation ? "1" : "0");
		jobValidation.put("JOB_VALIDATION_DESC", isJobValidation ? "有效" : "无效");
		return jobValidation;
	}

	/**
	 * 获取水样比对事件执行的任务有效性列表
	 * 
	 * @param resq
	 *            - { STATION_ID:站点编号, START_TIME:起始时间, END_TIME:结束时间,
	 *            PARAM_ID:污染物参数ID, JOB_ID:事件ID}
	 * @return - [{ TASK_CODE:任务编号, </br>
	 *         TASK_STATUS:任务状态, </br>
	 *         TASK_START_TIME:任务开始时间, </br>
	 *         TASK_END_TIME:任务截止时间, </br>
	 *         TASK_FINISH_TIME:任务完成时间, </br>
	 *         TASK_FINISH_BY:任务完成人, </br>
	 *         TASK_TIMELY:任务及时性, </br>
	 *         TASK_PASS: 任务合格性, </br>
	 *         TASK_VALIDATION:任务有效性 </br>
	 *         }]
	 */
	private List<Map<String, String>> getSampleComparisonTaskValidationList(Map<String, String> resqMap) {
		if (resqMap == null)
			return null;

		List<RowBase> dataList = waterSampleService.getSampleComparisonDataList(resqMap);
		if (dataList == null || dataList.size() < 1)
			return null;

		double dMin = 0.0;
		double dMax = 0.0;
		double dDeviation = 0.0;
		int nDATA_NUM = 0;
		int nDATA_PASSNUM = 0;
		boolean isSameTask = false;

		Map<String, String> tempTask = null;
		List<Map<String, String>> taskValidationList = new ArrayList<Map<String, String>>();
		for (int i = 0; i < dataList.size(); i++) {
			isSameTask = false;
			for (int j = 0; j < taskValidationList.size(); j++) {
				tempTask = taskValidationList.get(j);

				if (tempTask.get("TASK_CODE").equals(dataList.get(i).getSafeString("TASK_CODE"))) {

					isSameTask = true;

					// 获取合格偏差最大最小值
					dDeviation = Double.parseDouble(dataList.get(i).getSafeString("MEASURE_DEVIATION")); // dif
					dMin = Double.parseDouble(tempTask.get("MEASURE_MIN"));
					dMax = Double.parseDouble(tempTask.get("MEASURE_MAX"));

					if (dDeviation < dMin)
						dMin = dDeviation;

					if (dDeviation > dMax)
						dMax = dDeviation;

					tempTask.put("MEASURE_MIN", Double.toString(dMin));
					tempTask.put("MEASURE_MAX", Double.toString(dMax));

					// 获取测量次数和合格次数
					nDATA_NUM = Integer.parseInt(tempTask.get("DATA_NUM"));
					nDATA_PASSNUM = Integer.parseInt(tempTask.get("DATA_PASSNUM"));

					nDATA_NUM++;
					nDATA_PASSNUM += dataList.get(i).getSafeString("MEASURE_CONCLUSION").equals("1") ? 1 : 0; // dif

					tempTask.put("DATA_NUM", Integer.toString(nDATA_NUM));
					tempTask.put("DATA_PASSNUM", Integer.toString(nDATA_PASSNUM));

					taskValidationList.set(j, tempTask);
				}
			}

			if (isSameTask == false) {
				Map<String, String> task = cloneHashMap(resqMap);
				// 任务信息(通用)
				task.put("TASK_CODE", dataList.get(i).getSafeString("TASK_CODE"));
				task.put("TASK_STATUS", dataList.get(i).getSafeString("TASK_STATUS"));
				task.put("TASK_STATUS_DESC", dataList.get(i).getSafeString("TASK_STATUS_DESC"));
				task.put("TASK_START_TIME", dataList.get(i).getSafeString("TASK_START_TIME"));
				task.put("TASK_END_TIME", dataList.get(i).getSafeString("TASK_END_TIME"));
				task.put("TASK_FINISH_TIME", dataList.get(i).getSafeString("TASK_FINISH_TIME"));
				task.put("GROUP_NAME", dataList.get(i).getSafeString("GROUP_NAME"));
				task.put("USER_NAME", dataList.get(i).getSafeString("USER_NAME"));
				task.put("DATA_NUM", "1"); // 数据总数
				task.put("DATA_PASSNUM", dataList.get(i).getSafeString("MEASURE_CONCLUSION").equals("1") ? "1" : "0"); // dif
				// 记录信息(差异)
				task.put("MEASURE_MIN", dataList.get(i).getSafeString("MEASURE_DEVIATION")); // dif
				task.put("MEASURE_MAX", dataList.get(i).getSafeString("MEASURE_DEVIATION")); // dif
				taskValidationList.add(task);
			}
		}

		boolean isTaskPass = true; // 判断任务有效性
		for (int m = 0; m < taskValidationList.size(); m++) {
			tempTask = taskValidationList.get(m);

			nDATA_NUM = Integer.parseInt(tempTask.get("DATA_NUM"));
			nDATA_PASSNUM = Integer.parseInt(tempTask.get("DATA_PASSNUM"));

			if (nDATA_NUM < 3)
				isTaskPass = false;

			if ((nDATA_NUM - nDATA_PASSNUM) > 1)
				isTaskPass = false;

			tempTask.put("TASK_PASS", isTaskPass ? "1" : "0");
			tempTask.put("TASK_PASS_DESC", isTaskPass ? "合格" : "不合格");
			tempTask.put("TASK_TIMELY", tempTask.get("TASK_STATUS").equals("1") ? "1" : "0");
			tempTask.put("TASK_TIMELY_DESC", tempTask.get("TASK_STATUS").equals("1") ? "及时" : "不及时");
			taskValidationList.set(m, tempTask);
		}

		return taskValidationList;
	}
	
	
	/****************************************** 标样核查 **********************************************/
	/**
	 * 获取标样核查事件有效性
	 * 
	 * @param resqMap
	 * @return
	 */
	public Map<String, String> getStandardVerificationJobValidation(Map<String, String> resqMap) {
		if (resqMap == null)
			return null;

		List<Map<String, String>> taskValidationList = getStandardVerificationTaskValidationList(resqMap);
		if (taskValidationList == null || taskValidationList.size() < 1)
			return null;

		int nTotal = 0; // 总任务数
		int nComplete = 0; // 已完成任务数
		int nTimely = 0; // 及时完成任务数
		int nPass = 0; // 合格任务数
		boolean isJobValidation = true;

		nTotal = taskValidationList.size();
		for (int i = 0; i < taskValidationList.size(); i++) {
			Map<String, String> taskValidation = taskValidationList.get(i);
			String strTASK_STATUS = taskValidation.get("TASK_STATUS");
			String strTASK_PASS = taskValidation.get("TASK_PASS");

			if (strTASK_STATUS.equals("1") || strTASK_STATUS.equals("3")) // 按时完成+超时完成
				nComplete++;

			if (strTASK_STATUS.equals("1")) // 按时完成
				nTimely++;

			if (strTASK_PASS.equals("1")) // 合格
				nPass++;
		}

		// 有效性判断
		if ((nPass /(double)nTotal >= 0.75) && (nTimely /(double)nTotal >= 0.75) && (nComplete /(double)nTotal >= 0.9)) {
			isJobValidation = true;
		} else {
			isJobValidation = false;
		}

		Map<String, String> jobValidation = cloneHashMap(resqMap);
		jobValidation.put("JOB_TASK_TOTALNUM", Integer.toString(nTotal));
		jobValidation.put("JOB_TASK_COMPLETENUM", Integer.toString(nComplete));
		jobValidation.put("JOB_TASK_TIMELYNUM", Integer.toString(nTimely));
		jobValidation.put("JOB_TASK_PASSNUM", Integer.toString(nPass));
		jobValidation.put("JOB_VALIDATION", isJobValidation ? "1" : "0");
		jobValidation.put("JOB_VALIDATION_DESC", isJobValidation ? "有效" : "无效");
		return jobValidation;
	}
	
	/**
	 * 获取标样核查事件执行的任务有效性列表
	 * 
	 * @param params
	 * @return
	 */
	private List<Map<String, String>> getStandardVerificationTaskValidationList(Map<String, String> params) {
		if (params == null)
			return null;

		List<RowBase> dataList = standardVerificationService.getStandardVerificationDataList(params);
		if (dataList == null || dataList.size() < 1)
			return null;

		double dMin = 0.0;
		double dMax = 0.0;
		double dDeviation = 0.0;
		int nDATA_NUM = 0;
		int nDATA_PASSNUM = 0;
		boolean isSameTask = false;

		Map<String, String> tempTask = null;
		List<Map<String, String>> taskValidationList = new ArrayList<Map<String, String>>();
		for (int i = 0; i < dataList.size(); i++) {
			isSameTask = false;
			for (int j = 0; j < taskValidationList.size(); j++) {
				tempTask = taskValidationList.get(j);

				if (tempTask.get("TASK_CODE").equals(dataList.get(i).getSafeString("TASK_CODE"))) {
					isSameTask = true;
					// 获取合格偏差最大最小值
					dDeviation = Double.parseDouble(dataList.get(i).getSafeString("OPPOSITE_ERROR")); // dif
					dMin = Double.parseDouble(tempTask.get("MEASURE_MIN"));
					dMax = Double.parseDouble(tempTask.get("MEASURE_MAX"));

					if (dDeviation < dMin)
						dMin = dDeviation;

					if (dDeviation > dMax)
						dMax = dDeviation;

					tempTask.put("MEASURE_MIN", Double.toString(dMin));
					tempTask.put("MEASURE_MAX", Double.toString(dMax));

					// 获取测量次数和合格次数
					nDATA_NUM = Integer.parseInt(tempTask.get("DATA_NUM"));
					nDATA_PASSNUM = Integer.parseInt(tempTask.get("DATA_PASSNUM"));

					nDATA_NUM++;
					nDATA_PASSNUM += dataList.get(i).getSafeString("CONCLUSION").equals("1") ? 1 : 0; // dif

					tempTask.put("DATA_NUM", Integer.toString(nDATA_NUM));
					tempTask.put("DATA_PASSNUM", Integer.toString(nDATA_PASSNUM));

					taskValidationList.set(j, tempTask);
				}
			}

			if (isSameTask == false) {

				Map<String, String> task = cloneHashMap(params);
				// 任务信息(通用)
				task.put("TASK_CODE", dataList.get(i).getSafeString("TASK_CODE"));
				task.put("TASK_STATUS", dataList.get(i).getSafeString("TASK_STATUS"));
				task.put("TASK_STATUS_DESC", dataList.get(i).getSafeString("TASK_STATUS_DESC"));
				task.put("TASK_START_TIME", dataList.get(i).getSafeString("TASK_START_TIME"));
				task.put("TASK_END_TIME", dataList.get(i).getSafeString("TASK_END_TIME"));
				task.put("TASK_FINISH_TIME", dataList.get(i).getSafeString("TASK_FINISH_TIME"));
				task.put("GROUP_NAME", dataList.get(i).getSafeString("GROUP_NAME"));
				task.put("USER_NAME", dataList.get(i).getSafeString("USER_NAME"));
				task.put("DATA_NUM", "1"); // 数据总数
				task.put("DATA_PASSNUM", dataList.get(i).getSafeString("CONCLUSION").equals("1") ? "1" : "0"); // dif
				// 记录信息(差异)
				task.put("MEASURE_MIN", dataList.get(i).getSafeString("OPPOSITE_ERROR")); // dif
				task.put("MEASURE_MAX", dataList.get(i).getSafeString("OPPOSITE_ERROR")); // dif
				taskValidationList.add(task);
			}
		}

		boolean isTaskPass = true; // 判断任务有效性
		for (int m = 0; m < taskValidationList.size(); m++) {
			tempTask = taskValidationList.get(m);

			nDATA_NUM = Integer.parseInt(tempTask.get("DATA_NUM"));
			nDATA_PASSNUM = Integer.parseInt(tempTask.get("DATA_PASSNUM"));

			if (nDATA_NUM < 3)
				isTaskPass = false;

			if ((nDATA_NUM - nDATA_PASSNUM) > 1)
				isTaskPass = false;

			tempTask.put("TASK_PASS", isTaskPass ? "1" : "0");
			tempTask.put("TASK_PASS_DESC", isTaskPass ? "合格" : "不合格");
			tempTask.put("TASK_TIMELY", tempTask.get("TASK_STATUS").equals("1") ? "1" : "0");
			tempTask.put("TASK_TIMELY_DESC", tempTask.get("TASK_STATUS").equals("1") ? "及时" : "不及时");
			taskValidationList.set(m, tempTask);
		}

		return taskValidationList;
	}
	
	
	/****************************************** 标准物质更换 **********************************************/
	/**
	 * 获取标准物质更换事件有效性
	 * 
	 * @param params
	 * @return
	 */
	public Map<String, String> getStandardSubstanceJobValidation(Map<String, String> params) {
		if (params == null)
			return null;

		List<Map<String, String>> taskValidationList = getStandardSubstanceTaskValidationList(params);
		if (taskValidationList == null || taskValidationList.size() < 1)
			return null;

		int nTotal = 0; // 总任务数
		int nComplete = 0; // 已完成任务数
		int nTimely = 0; // 及时完成任务数
		int nPass = 0; // 合格任务数
		boolean isJobValidation = true;

		nTotal = taskValidationList.size();
		for (int i = 0; i < taskValidationList.size(); i++) {
			Map<String, String> taskValidation = taskValidationList.get(i);
			String strTASK_STATUS = taskValidation.get("TASK_STATUS");
			String strTASK_PASS = taskValidation.get("TASK_PASS");

			if (strTASK_STATUS.equals("1") || strTASK_STATUS.equals("3")) // 按时完成+超时完成
				nComplete++;

			if (strTASK_STATUS.equals("1")) // 按时完成
				nTimely++;

			if (strTASK_PASS.equals("1")) // 合格
				nPass++;
		}

		// 有效性判断
		if ((nPass /(double)nTotal >= 0.75) && (nTimely /(double)nTotal >= 0.75) && (nComplete /(double)nTotal >= 0.9)) {
			isJobValidation = true;
		} else {
			isJobValidation = false;
		}

		Map<String, String> jobValidation = cloneHashMap(params);
		jobValidation.put("JOB_TASK_TOTALNUM", Integer.toString(nTotal));
		jobValidation.put("JOB_TASK_COMPLETENUM", Integer.toString(nComplete));
		jobValidation.put("JOB_TASK_TIMELYNUM", Integer.toString(nTimely));
		jobValidation.put("JOB_TASK_PASSNUM", Integer.toString(nPass));
		jobValidation.put("JOB_VALIDATION", isJobValidation ? "1" : "0");
		jobValidation.put("JOB_VALIDATION_DESC", isJobValidation ? "有效" : "无效");
		return jobValidation;
	}

	/**
	 * 获取标准物质更换事件执行的任务有效性列表
	 * 
	 * @param resqMap
	 * @return
	 */
	private List<Map<String, String>> getStandardSubstanceTaskValidationList(Map<String, String> resqMap) {
		if (resqMap == null)
			return null;

		List<RowBase> dataList = standardSubstanceService.getStandardSubstanceDataList(resqMap);
		if (dataList == null || dataList.size() < 1)
			return null;

		int nDATA_NUM = 0;
		int nDATA_PASSNUM = 0;
		int nQUANTITY = 0;
		boolean isSameTask = false;

		Map<String, String> tempTask = null;
		List<Map<String, String>> taskValidationList = new ArrayList<Map<String, String>>();
		for (int i = 0; i < dataList.size(); i++) {
			isSameTask = false;
			for (int j = 0; j < taskValidationList.size(); j++) {
				tempTask = taskValidationList.get(j);

				if (tempTask.get("TASK_CODE").equals(dataList.get(i).getSafeString("TASK_CODE"))) {
					isSameTask = true;
					// 获取更换次数和更换数量
					nDATA_NUM = Integer.parseInt(tempTask.get("DATA_NUM"));
					nDATA_PASSNUM = Integer.parseInt(tempTask.get("DATA_PASSNUM"));
					nQUANTITY = Integer.parseInt(tempTask.get("QUANTITY"));

					nDATA_NUM++;
					nDATA_PASSNUM += Integer.parseInt(dataList.get(i).getSafeString("QUANTITY")) > 0 ? 1 : 0; // dif
					nQUANTITY += Integer.parseInt(dataList.get(i).getSafeString("QUANTITY"));

					tempTask.put("DATA_NUM", Integer.toString(nDATA_NUM));
					tempTask.put("DATA_PASSNUM", Integer.toString(nDATA_PASSNUM));
					tempTask.put("QUANTITY", Integer.toString(nQUANTITY));

					taskValidationList.set(j, tempTask);
				}
			}

			if (isSameTask == false) {

				Map<String, String> task = cloneHashMap(resqMap);
				// 任务信息(通用)
				task.put("TASK_CODE", dataList.get(i).getSafeString("TASK_CODE"));
				task.put("TASK_STATUS", dataList.get(i).getSafeString("TASK_STATUS"));
				task.put("TASK_STATUS_DESC", dataList.get(i).getSafeString("TASK_STATUS_DESC"));
				task.put("TASK_START_TIME", dataList.get(i).getSafeString("TASK_START_TIME"));
				task.put("TASK_END_TIME", dataList.get(i).getSafeString("TASK_END_TIME"));
				task.put("TASK_FINISH_TIME", dataList.get(i).getSafeString("TASK_FINISH_TIME"));
				task.put("GROUP_NAME", dataList.get(i).getSafeString("GROUP_NAME"));
				task.put("USER_NAME", dataList.get(i).getSafeString("USER_NAME"));
				task.put("DATA_NUM", "1"); // 数据总数
				task.put("DATA_PASSNUM", Integer.parseInt(dataList.get(i).getSafeString("QUANTITY")) > 0 ? "1" : "0"); // dif
				// 记录信息(差异)
				task.put("STAND_NAME", dataList.get(i).getSafeString("STAND_NAME")); // dif
				task.put("QUANTITY", dataList.get(i).getSafeString("QUANTITY")); // dif
				taskValidationList.add(task);
			}
		}

		boolean isTaskPass = true; // 判断任务有效性
		for (int m = 0; m < taskValidationList.size(); m++) {
			tempTask = taskValidationList.get(m);

			nDATA_NUM = Integer.parseInt(tempTask.get("DATA_NUM"));
			nDATA_PASSNUM = Integer.parseInt(tempTask.get("DATA_PASSNUM"));

			if (nDATA_NUM < 1)
				isTaskPass = false;

			tempTask.put("TASK_PASS", isTaskPass ? "1" : "0");
			tempTask.put("TASK_PASS_DESC", isTaskPass ? "完成" : "未完成");
			tempTask.put("TASK_TIMELY", tempTask.get("TASK_STATUS").equals("1") ? "1" : "0");
			tempTask.put("TASK_TIMELY_DESC", tempTask.get("TASK_STATUS").equals("1") ? "及时" : "不及时");
			taskValidationList.set(m, tempTask);
		}

		return taskValidationList;
	}



	/****************************************** 超标比对 **********************************************/
	/**
	 * 获取超标比对事件有效性
	 * 
	 * @param params
	 * @return
	 */
	public Map<String, String> getExceedComparisonJobValidation(Map<String, String> params) {
		if (params == null)
			return null;

		List<Map<String, String>> taskValidationList = getExceedComparisonTaskValidationList(params);
		if (taskValidationList == null || taskValidationList.size() < 1)
			return null;

		int nTotal = 0; // 总任务数
		int nComplete = 0; // 已完成任务数
		int nTimely = 0; // 及时完成任务数
		int nPass = 0; // 合格任务数
		boolean isJobValidation = true;

		nTotal = taskValidationList.size();
		for (int i = 0; i < taskValidationList.size(); i++) {
			Map<String, String> taskValidation = taskValidationList.get(i);
			String strTASK_STATUS = taskValidation.get("TASK_STATUS");
			String strTASK_PASS = taskValidation.get("TASK_PASS");

			if (strTASK_STATUS.equals("1") || strTASK_STATUS.equals("3")) // 按时完成+超时完成
				nComplete++;

			if (strTASK_STATUS.equals("1")) // 按时完成
				nTimely++;

			if (strTASK_PASS.equals("1")) // 合格
				nPass++;
		}

		// 有效性判断
		if ((nPass /(double)nTotal >= 0.75) && (nTimely /(double)nTotal >= 0.75) && (nComplete /(double)nTotal >= 0.9)) {
			isJobValidation = true;
		} else {
			isJobValidation = false;
		}

		Map<String, String> jobValidation = cloneHashMap(params);
		jobValidation.put("JOB_TASK_TOTALNUM", Integer.toString(nTotal));
		jobValidation.put("JOB_TASK_COMPLETENUM", Integer.toString(nComplete));
		jobValidation.put("JOB_TASK_TIMELYNUM", Integer.toString(nTimely));
		jobValidation.put("JOB_TASK_PASSNUM", Integer.toString(nPass));
		jobValidation.put("JOB_VALIDATION", isJobValidation ? "1" : "0");
		jobValidation.put("JOB_VALIDATION_DESC", isJobValidation ? "有效" : "无效");
		return jobValidation;
	}

	/**
	 * 获取超标比对事件执行的任务有效性列表
	 * 
	 * @param params
	 * @return
	 */
	private List<Map<String, String>> getExceedComparisonTaskValidationList(Map<String, String> params) {
		if (params == null)
			return null;

		List<RowBase> dataList = exceedComparisonService.getExceedComparisonDataList(params);
		if (dataList == null || dataList.size() < 1)
			return null;

		double dMin = 0.0;
		double dMax = 0.0;
		double dDeviation = 0.0;
		int nDATA_NUM = 0;
		int nDATA_PASSNUM = 0;
		boolean isSameTask = false;

		Map<String, String> tempTask = null;
		List<Map<String, String>> taskValidationList = new ArrayList<Map<String, String>>();
		for (int i = 0; i < dataList.size(); i++) {
			isSameTask = false;
			for (int j = 0; j < taskValidationList.size(); j++) {
				tempTask = taskValidationList.get(j);

				if (tempTask.get("TASK_CODE").equals(dataList.get(i).getSafeString("TASK_CODE"))) {
					isSameTask = true;
					// 获取合格偏差最大最小值
					dDeviation = Double.parseDouble(dataList.get(i).getSafeString("MEASURE_DEVIATION")); // dif
					dMin = Double.parseDouble(tempTask.get("MEASURE_MIN"));
					dMax = Double.parseDouble(tempTask.get("MEASURE_MAX"));

					if (dDeviation < dMin)
						dMin = dDeviation;

					if (dDeviation > dMax)
						dMax = dDeviation;

					tempTask.put("MEASURE_MIN", Double.toString(dMin));
					tempTask.put("MEASURE_MAX", Double.toString(dMax));

					// 获取测量次数和合格次数
					nDATA_NUM = Integer.parseInt(tempTask.get("DATA_NUM"));
					nDATA_PASSNUM = Integer.parseInt(tempTask.get("DATA_PASSNUM"));

					nDATA_NUM++;
					nDATA_PASSNUM += dataList.get(i).getSafeString("MEASURE_CONCLUSION").equals("1") ? 1 : 0; // dif

					tempTask.put("DATA_NUM", Integer.toString(nDATA_NUM));
					tempTask.put("DATA_PASSNUM", Integer.toString(nDATA_PASSNUM));

					taskValidationList.set(j, tempTask);
				}
			}

			if (isSameTask == false) {

				Map<String, String> task = cloneHashMap(params);
				// 任务信息(通用)
				task.put("TASK_CODE", dataList.get(i).getSafeString("TASK_CODE"));
				task.put("TASK_STATUS", dataList.get(i).getSafeString("TASK_STATUS"));
				task.put("TASK_STATUS_DESC", dataList.get(i).getSafeString("TASK_STATUS_DESC"));
				task.put("TASK_START_TIME", dataList.get(i).getSafeString("TASK_START_TIME"));
				task.put("TASK_END_TIME", dataList.get(i).getSafeString("TASK_END_TIME"));
				task.put("TASK_FINISH_TIME", dataList.get(i).getSafeString("TASK_FINISH_TIME"));
				task.put("GROUP_NAME", dataList.get(i).getSafeString("GROUP_NAME"));
				task.put("USER_NAME", dataList.get(i).getSafeString("USER_NAME"));
				task.put("DATA_NUM", "1"); // 数据总数
				task.put("DATA_PASSNUM", dataList.get(i).getSafeString("MEASURE_CONCLUSION").equals("1") ? "1" : "0"); // dif
				// 记录信息(差异)
				task.put("MEASURE_MIN", dataList.get(i).getSafeString("MEASURE_DEVIATION")); // dif
				task.put("MEASURE_MAX", dataList.get(i).getSafeString("MEASURE_DEVIATION")); // dif
				taskValidationList.add(task);
			}
		}

		boolean isTaskPass = true; // 判断任务有效性
		for (int m = 0; m < taskValidationList.size(); m++) {
			tempTask = taskValidationList.get(m);

			nDATA_NUM = Integer.parseInt(tempTask.get("DATA_NUM"));
			nDATA_PASSNUM = Integer.parseInt(tempTask.get("DATA_PASSNUM"));

			if (nDATA_NUM < 3)
				isTaskPass = false;

			if ((nDATA_NUM - nDATA_PASSNUM) > 1)
				isTaskPass = false;

			tempTask.put("TASK_PASS", isTaskPass ? "1" : "0");
			tempTask.put("TASK_PASS_DESC", isTaskPass ? "合格" : "不合格");
			tempTask.put("TASK_TIMELY", tempTask.get("TASK_STATUS").equals("1") ? "1" : "0");
			tempTask.put("TASK_TIMELY_DESC", tempTask.get("TASK_STATUS").equals("1") ? "及时" : "不及时");
			taskValidationList.set(m, tempTask);
		}

		return taskValidationList;
	}

	/****************************************** 设备故障维修 **********************************************/
	/**
	 * 获取故障维修事件有效性
	 * 
	 * @param params
	 * @return
	 */
	public Map<String, String> getFailureRepairJobValidation(Map<String, String> params) {
		if (params == null)
			return null;

		List<Map<String, String>> taskValidationList = getFailureRepairTaskValidationList(params);
		if (taskValidationList == null || taskValidationList.size() < 1)
			return null;

		int nTotal = 0; // 总任务数
		int nComplete = 0; // 已完成任务数
		int nTimely = 0; // 及时完成任务数
		int nPass = 0; // 合格任务数
		boolean isJobValidation = true;

		nTotal = taskValidationList.size();
		for (int i = 0; i < taskValidationList.size(); i++) {
			Map<String, String> taskValidation = taskValidationList.get(i);
			String strTASK_STATUS = taskValidation.get("TASK_STATUS");
			String strTASK_PASS = taskValidation.get("TASK_PASS");

			if (strTASK_STATUS.equals("1") || strTASK_STATUS.equals("3")) // 按时完成+超时完成
				nComplete++;

			if (strTASK_STATUS.equals("1")) // 按时完成
				nTimely++;

			if (strTASK_PASS.equals("1")) // 合格
				nPass++;
		}

		// 有效性判断
		if ((nPass /(double)nTotal >= 0.75) && (nTimely /(double)nTotal >= 0.75) && (nComplete /(double)nTotal >= 0.9)) {
			isJobValidation = true;
		} else {
			isJobValidation = false;
		}

		Map<String, String> jobValidation = cloneHashMap(params);
		jobValidation.put("JOB_TASK_TOTALNUM", Integer.toString(nTotal));
		jobValidation.put("JOB_TASK_COMPLETENUM", Integer.toString(nComplete));
		jobValidation.put("JOB_TASK_TIMELYNUM", Integer.toString(nTimely));
		jobValidation.put("JOB_TASK_PASSNUM", Integer.toString(nPass));
		jobValidation.put("JOB_VALIDATION", isJobValidation ? "1" : "0");
		jobValidation.put("JOB_VALIDATION_DESC", isJobValidation ? "有效" : "无效");
		return jobValidation;
	}

	/**
	 * 获取故障维修事件执行的任务有效性列表
	 * 
	 * @param resqMap
	 * @return
	 */
	private List<Map<String, String>> getFailureRepairTaskValidationList(Map<String, String> resqMap) {
		if (resqMap == null)
			return null;

		List<RowBase> dataList = failureRepairService.getFailureRepairDataList(resqMap);
		if (dataList == null || dataList.size() < 1)
			return null;

		List<Map<String, String>> taskValidationList = new ArrayList<Map<String, String>>();
		for (int i = 0; i < dataList.size(); i++) {

			Map<String, String> task = cloneHashMap(resqMap);
			// 任务信息(通用)
			task.put("TASK_CODE", dataList.get(i).getSafeString("TASK_CODE"));
			task.put("TASK_STATUS", dataList.get(i).getSafeString("TASK_STATUS"));
			task.put("TASK_STATUS_DESC", dataList.get(i).getSafeString("TASK_STATUS_DESC"));
			task.put("TASK_START_TIME", dataList.get(i).getSafeString("TASK_START_TIME"));
			task.put("TASK_END_TIME", dataList.get(i).getSafeString("TASK_END_TIME"));
			task.put("TASK_FINISH_TIME", dataList.get(i).getSafeString("TASK_FINISH_TIME"));
			task.put("GROUP_NAME", dataList.get(i).getSafeString("GROUP_NAME"));
			task.put("USER_NAME", dataList.get(i).getSafeString("USER_NAME"));
			task.put("DATA_NUM", "1"); // 默认1
			task.put("DATA_PASSNUM", "1"); // dif
			task.put("TASK_PASS", dataList.get(i).get("REPAIR_RESULT").equals("1") ? "1" : "0"); // dif
			task.put("TASK_PASS_DESC", dataList.get(i).get("REPAIR_RESULT").equals("1") ? "解决" : "未解决"); // dif
			task.put("TASK_TIMELY", dataList.get(i).getSafeString("TASK_STATUS").equals("1") ? "1" : "0");
			task.put("TASK_TIMELY_DESC", dataList.get(i).getSafeString("TASK_STATUS").equals("1") ? "及时" : "不及时");
			// 记录信息(差异)
			task.put("DEVICE_NAME", dataList.get(i).getSafeString("DEVICE_NAME")); // dif
			task.put("FAILURE_REASON_DESC", dataList.get(i).getSafeString("FAILURE_REASON_DESC")); // dif

			taskValidationList.add(task);
		}

		return taskValidationList;
	}

	

	/****************************************** 备件耗材更换 **********************************************/
	/**
	 * 获取备件耗材更换事件有效性
	 * 
	 * @param resqMap
	 * @return
	 */
	public Map<String, String> getMaterialReplaceJobValidation(Map<String, String> resqMap) {
		if (resqMap == null)
			return null;

		List<Map<String, String>> taskValidationList = getMaterialReplaceTaskValidationList(resqMap);
		if (taskValidationList == null || taskValidationList.size() < 1)
			return null;

		int nTotal = 0; // 总任务数
		int nComplete = 0; // 已完成任务数
		int nTimely = 0; // 及时完成任务数
		int nPass = 0; // 合格任务数
		boolean isJobValidation = true;

		nTotal = taskValidationList.size();
		for (int i = 0; i < taskValidationList.size(); i++) {
			Map<String, String> taskValidation = taskValidationList.get(i);
			String strTASK_STATUS = taskValidation.get("TASK_STATUS");
			String strTASK_PASS = taskValidation.get("TASK_PASS");

			if (strTASK_STATUS.equals("1") || strTASK_STATUS.equals("3")) // 按时完成+超时完成
				nComplete++;

			if (strTASK_STATUS.equals("1")) // 按时完成
				nTimely++;

			if (strTASK_PASS.equals("1")) // 合格
				nPass++;
		}

		// 有效性判断
		if ((nPass /(double)nTotal >= 0.75) && (nTimely /(double)nTotal >= 0.75) && (nComplete /(double)nTotal >= 0.9)) {
			isJobValidation = true;
		} else {
			isJobValidation = false;
		}

		Map<String, String> jobValidation = cloneHashMap(resqMap);
		jobValidation.put("JOB_TASK_TOTALNUM", Integer.toString(nTotal));
		jobValidation.put("JOB_TASK_COMPLETENUM", Integer.toString(nComplete));
		jobValidation.put("JOB_TASK_TIMELYNUM", Integer.toString(nTimely));
		jobValidation.put("JOB_TASK_PASSNUM", Integer.toString(nPass));
		jobValidation.put("JOB_VALIDATION", isJobValidation ? "1" : "0");
		jobValidation.put("JOB_VALIDATION_DESC", isJobValidation ? "有效" : "无效");
		return jobValidation;
	}

	/**
	 * 获取备件耗材更换事件执行的任务有效性列表
	 * 
	 * @param params
	 * @return
	 */
	private List<Map<String, String>> getMaterialReplaceTaskValidationList(Map<String, String> params) {
		if (params == null)
			return null;

		List<RowBase> dataList = materialReplaceService.getMaterialReplaceDataList(params);
		if (dataList == null || dataList.size() < 1)
			return null;

		int nDATA_NUM = 0;
		int nDATA_PASSNUM = 0;
		int nQUANTITY = 0;
		boolean isSameTask = false;

		Map<String, String> tempTask = null;
		List<Map<String, String>> taskValidationList = new ArrayList<Map<String, String>>();
		for (int i = 0; i < dataList.size(); i++) {
			isSameTask = false;
			for (int j = 0; j < taskValidationList.size(); j++) {
				tempTask = taskValidationList.get(j);

				if (tempTask.get("TASK_CODE").equals(dataList.get(i).getSafeString("TASK_CODE"))) {
					isSameTask = true;

					// 获取更换次数和更换数量
					nDATA_NUM = Integer.parseInt(tempTask.get("DATA_NUM"));
					nDATA_PASSNUM = Integer.parseInt(tempTask.get("DATA_PASSNUM"));
					nQUANTITY = Integer.parseInt(tempTask.get("MATERIAL_QUANTITY"));

					nDATA_NUM++;
					nDATA_PASSNUM += dataList.get(i).getSafeString("MATERIAL_QUANTITY").length() > 0 ? 1 : 0; // dif
					nQUANTITY += Integer.parseInt(dataList.get(i).getSafeString("MATERIAL_QUANTITY"));

					tempTask.put("DATA_NUM", Integer.toString(nDATA_NUM));
					tempTask.put("DATA_PASSNUM", Integer.toString(nDATA_PASSNUM));
					tempTask.put("MATERIAL_QUANTITY", Integer.toString(nQUANTITY));

					taskValidationList.set(j, tempTask);
				}
			}
			if (isSameTask == false) {

				Map<String, String> task = cloneHashMap(params);
				// 任务信息(通用)
				task.put("TASK_CODE", dataList.get(i).getSafeString("TASK_CODE"));
				task.put("TASK_STATUS", dataList.get(i).getSafeString("TASK_STATUS"));
				task.put("TASK_STATUS_DESC", dataList.get(i).getSafeString("TASK_STATUS_DESC"));
				task.put("TASK_START_TIME", dataList.get(i).getSafeString("TASK_START_TIME"));
				task.put("TASK_END_TIME", dataList.get(i).getSafeString("TASK_END_TIME"));
				task.put("TASK_FINISH_TIME", dataList.get(i).getSafeString("TASK_FINISH_TIME"));
				task.put("GROUP_NAME", dataList.get(i).getSafeString("GROUP_NAME"));
				task.put("USER_NAME", dataList.get(i).getSafeString("USER_NAME"));
				task.put("DATA_NUM", "1"); // 数据总数
				task.put("DATA_PASSNUM",
						Integer.parseInt(dataList.get(i).getSafeString("MATERIAL_QUANTITY")) > 0 ? "1" : "0"); // dif
				// 记录信息(差异)
				task.put("MATERIAL_NAME_DESC", dataList.get(i).getSafeString("MATERIAL_NAME_DESC")); // dif
				task.put("MATERIAL_QUANTITY", dataList.get(i).getSafeString("MATERIAL_QUANTITY")); // dif
				task.put("MATERIAL_UNIT", dataList.get(i).getSafeString("MATERIAL_UNIT")); // dif
				taskValidationList.add(task);
			}
		}

		boolean isTaskPass = true; // 判断任务有效性
		for (int m = 0; m < taskValidationList.size(); m++) {
			tempTask = taskValidationList.get(m);

			nDATA_NUM = Integer.parseInt(tempTask.get("DATA_NUM"));
			nDATA_PASSNUM = Integer.parseInt(tempTask.get("DATA_PASSNUM"));

			if (nDATA_NUM < 1)
				isTaskPass = false;

			tempTask.put("TASK_PASS", isTaskPass ? "1" : "0");
			tempTask.put("TASK_PASS_DESC", isTaskPass ? "完成" : "未完成");
			tempTask.put("TASK_TIMELY", tempTask.get("TASK_STATUS").equals("1") ? "1" : "0");
			tempTask.put("TASK_TIMELY_DESC", tempTask.get("TASK_STATUS").equals("1") ? "及时" : "不及时");

			taskValidationList.set(m, tempTask);
		}

		return taskValidationList;
	}

	/****************************************** 人工监测 **********************************************/
	/**
	 * 获取人工监测事件有效性
	 * 
	 * @param resqMap
	 * @return
	 */
	public Map<String, String> getArtificialMonitorJobValidation(Map<String, String> resqMap) {
		if (resqMap == null)
			return null;

		List<Map<String, String>> taskValidationList = getArtificialMonitorTaskValidationList(resqMap);
		if (taskValidationList == null || taskValidationList.size() < 1)
			return null;

		int nTotal = 0; // 总任务数
		int nComplete = 0; // 已完成任务数
		int nTimely = 0; // 及时完成任务数
		int nPass = 0; // 合格任务数
		boolean isJobValidation = true;

		nTotal = taskValidationList.size();
		for (int i = 0; i < taskValidationList.size(); i++) {
			Map<String, String> taskValidation = taskValidationList.get(i);
			String strTASK_STATUS = taskValidation.get("TASK_STATUS");
			String strTASK_PASS = taskValidation.get("TASK_PASS");

			if (strTASK_STATUS.equals("1") || strTASK_STATUS.equals("3")) // 按时完成+超时完成
				nComplete++;

			if (strTASK_STATUS.equals("1")) // 按时完成
				nTimely++;

			if (strTASK_PASS.equals("1")) // 合格
				nPass++;
		}

		// 有效性判断
		if ((nPass /(double)nTotal >= 0.75) && (nTimely /(double)nTotal >= 0.75) && (nComplete /(double)nTotal >= 0.9)) {
			isJobValidation = true;
		} else {
			isJobValidation = false;
		}

		Map<String, String> jobValidation = cloneHashMap(resqMap);
		jobValidation.put("JOB_TASK_TOTALNUM", Integer.toString(nTotal));
		jobValidation.put("JOB_TASK_COMPLETENUM", Integer.toString(nComplete));
		jobValidation.put("JOB_TASK_TIMELYNUM", Integer.toString(nTimely));
		jobValidation.put("JOB_TASK_PASSNUM", Integer.toString(nPass));
		jobValidation.put("JOB_VALIDATION", isJobValidation ? "1" : "0");
		jobValidation.put("JOB_VALIDATION_DESC", isJobValidation ? "有效" : "无效");
		return jobValidation;
	}

	/**
	 * 获取人工监测事件执行的任务有效性列表
	 * 
	 * @param params
	 * @return
	 */
	private List<Map<String, String>> getArtificialMonitorTaskValidationList(Map<String, String> params) {
		if (params == null)
			return null;

		List<RowBase> dataList = artificialMonitorService.getArtificialMonitorDataList(params);
		if (dataList == null || dataList.size() < 1)
			return null;

		int nDATA_NUM = 0;
		int nDATA_PASSNUM = 0;
		boolean isSameTask = false;

		Map<String, String> tempTask = null;
		List<Map<String, String>> taskValidationList = new ArrayList<Map<String, String>>();
		for (int i = 0; i < dataList.size(); i++) {
			isSameTask = false;
			for (int j = 0; j < taskValidationList.size(); j++) {
				tempTask = taskValidationList.get(j);

				if (tempTask.get("TASK_CODE").equals(dataList.get(i).getSafeString("TASK_CODE"))) {
					isSameTask = true;

					// 获取测量次数和合格次数
					nDATA_NUM = Integer.parseInt(tempTask.get("DATA_NUM"));
					nDATA_PASSNUM = Integer.parseInt(tempTask.get("DATA_PASSNUM"));

					nDATA_NUM++;
					nDATA_PASSNUM += dataList.get(i).getSafeString("MONITOR_VALUE").length() > 0 ? 1 : 0; // dif

					tempTask.put("DATA_NUM", Integer.toString(nDATA_NUM));
					tempTask.put("DATA_PASSNUM", Integer.toString(nDATA_PASSNUM));

					taskValidationList.set(j, tempTask);
				}
			}
			if (isSameTask == false) {

				Map<String, String> task = cloneHashMap(params);
				// 任务信息(通用)
				task.put("TASK_CODE", dataList.get(i).getSafeString("TASK_CODE"));
				task.put("TASK_STATUS", dataList.get(i).getSafeString("TASK_STATUS"));
				task.put("TASK_STATUS_DESC", dataList.get(i).getSafeString("TASK_STATUS_DESC"));
				task.put("TASK_START_TIME", dataList.get(i).getSafeString("TASK_START_TIME"));
				task.put("TASK_END_TIME", dataList.get(i).getSafeString("TASK_END_TIME"));
				task.put("TASK_FINISH_TIME", dataList.get(i).getSafeString("TASK_FINISH_TIME"));
				task.put("GROUP_NAME", dataList.get(i).getSafeString("GROUP_NAME"));
				task.put("USER_NAME", dataList.get(i).getSafeString("USER_NAME"));
				task.put("DATA_NUM", "1"); // 数据总数
				task.put("DATA_PASSNUM", dataList.get(i).getSafeString("MONITOR_VALUE").length() > 0 ? "1" : "0"); // dif

				taskValidationList.add(task);
			}
		}

		boolean isTaskPass = true; // 判断任务有效性
		for (int m = 0; m < taskValidationList.size(); m++) {
			tempTask = taskValidationList.get(m);

			nDATA_NUM = Integer.parseInt(tempTask.get("DATA_NUM"));
			nDATA_PASSNUM = Integer.parseInt(tempTask.get("DATA_PASSNUM"));

			if (nDATA_NUM < 1)
				isTaskPass = false;

			tempTask.put("TASK_PASS", isTaskPass ? "1" : "0");
			tempTask.put("TASK_PASS_DESC", isTaskPass ? "完成" : "未完成");
			tempTask.put("TASK_TIMELY", tempTask.get("TASK_STATUS").equals("1") ? "1" : "0");
			tempTask.put("TASK_TIMELY_DESC", tempTask.get("TASK_STATUS").equals("1") ? "及时" : "不及时");
			taskValidationList.set(m, tempTask);
		}

		return taskValidationList;
	}

	/****************************************** 日常巡检 **********************************************/
	/**
	 * 获取日常巡检事件有效性
	 * 
	 * @param params
	 * @return
	 */
	public Map<String, String> getDailyInspectionJobValidation(Map<String, String> params) {
		if (params == null)
			return null;

		List<Map<String, String>> taskValidationList = getDailyInspectionTaskValidationList(params);
		if (taskValidationList == null || taskValidationList.size() < 1)
			return null;

		int nTotal = 0; // 总任务数
		int nComplete = 0; // 已完成任务数
		int nTimely = 0; // 及时完成任务数
		int nPass = 0; // 合格任务数
		boolean isJobValidation = true;

		nTotal = taskValidationList.size();
		for (int i = 0; i < taskValidationList.size(); i++) {
			Map<String, String> taskValidation = taskValidationList.get(i);
			String strTASK_STATUS = taskValidation.get("TASK_STATUS");
			String strTASK_PASS = taskValidation.get("TASK_PASS");

			if (strTASK_STATUS.equals("1") || strTASK_STATUS.equals("3")) // 按时完成+超时完成
				nComplete++;

			if (strTASK_STATUS.equals("1")) // 按时完成
				nTimely++;

			if (strTASK_PASS.equals("1")) // 合格
				nPass++;
		}

		// 有效性判断
		if ((nPass /(double)nTotal >= 0.75) && (nTimely /(double)nTotal >= 0.75) && (nComplete /(double)nTotal >= 0.9)) {
			isJobValidation = true;
		} else {
			isJobValidation = false;
		}

		Map<String, String> jobValidation = cloneHashMap(params);
		jobValidation.put("JOB_TASK_TOTALNUM", Integer.toString(nTotal));
		jobValidation.put("JOB_TASK_COMPLETENUM", Integer.toString(nComplete));
		jobValidation.put("JOB_TASK_TIMELYNUM", Integer.toString(nTimely));
		jobValidation.put("JOB_TASK_PASSNUM", Integer.toString(nPass));
		jobValidation.put("JOB_VALIDATION", isJobValidation ? "1" : "0");
		jobValidation.put("JOB_VALIDATION_DESC", isJobValidation ? "有效" : "无效");
		return jobValidation;
	}

	/**
	 * 获取日常巡检事件执行的任务有效性列表
	 * 
	 * @param params 参数需要包含：STATION_ID, JOB_ID:日常巡检的JOB_ID, START_TIME, END_TIME
	 * @return
	 */
	private List<Map<String, String>> getDailyInspectionTaskValidationList(Map<String, String> params) {
		if (params == null)
			return null;

		List<RowBase> dataList = dailyInspectionService.getDailyInspectionDataList(params);
		if (dataList == null || dataList.size() < 1)
			return null;

		int nDATA_NUM = 0;
		int nDATA_PASSNUM = 0;

		List<Map<String, String>> taskValidationList = new ArrayList<Map<String, String>>();
		for (int i = 0; i < dataList.size(); i++) {
			nDATA_NUM = 0;
			nDATA_PASSNUM = 0;
			// 计算检查项通过的数量
			RowBase row = dataList.get(i);
			String[] cols = row.getColumns();
			for (int j = 0; j < cols.length; j++) {
				if (cols[j].length() > 3)
					continue;
				nDATA_NUM++;
				nDATA_PASSNUM += row.getSafeString(cols[j]).equals("1") ? 1 : 0; // dif
			}

			Map<String, String> task = cloneHashMap(params);
			// 任务信息(通用)
			task.put("TASK_CODE", dataList.get(i).getSafeString("TASK_CODE"));
			task.put("TASK_STATUS", dataList.get(i).getSafeString("TASK_STATUS"));
			task.put("TASK_STATUS_DESC", dataList.get(i).getSafeString("TASK_STATUS_DESC"));
			task.put("TASK_START_TIME", dataList.get(i).getSafeString("TASK_START_TIME"));
			task.put("TASK_END_TIME", dataList.get(i).getSafeString("TASK_END_TIME"));
			task.put("TASK_FINISH_TIME", dataList.get(i).getSafeString("TASK_FINISH_TIME"));
			task.put("GROUP_NAME", dataList.get(i).getSafeString("GROUP_NAME"));
			task.put("USER_NAME", dataList.get(i).getSafeString("USER_NAME"));
			task.put("DATA_NUM", Integer.toString(nDATA_NUM)); // 检查项总数
			task.put("DATA_PASSNUM", Integer.toString(nDATA_PASSNUM)); // dif
			task.put("TASK_PASS", nDATA_PASSNUM == nDATA_NUM ? "1" : "0"); // dif
			task.put("TASK_PASS_DESC", nDATA_PASSNUM == nDATA_NUM ? "通过" : "不通过"); // dif
			task.put("TASK_TIMELY", dataList.get(i).getSafeString("TASK_STATUS").equals("1") ? "1" : "0");
			task.put("TASK_TIMELY_DESC", dataList.get(i).getSafeString("TASK_STATUS").equals("1") ? "及时" : "不及时");
			taskValidationList.add(task);
		}

		return taskValidationList;
	}
	
	/**
	 * 获取日常巡检分类统计结果
	 * 
	 * @param params
	 *            { STATION_ID:站点编号, START_TIME:起始时间, END_TIME:结束时间,
	 *            PARAM_ID:污染物参数 （无）, JOB_ID:事件ID, TYPE_CODE:日常巡检分类, }
	 * @return { JOB_ID:事件编号, </br>
	 *         JOB_NAME:事件名称, </br>
	 *         JOB_TYPE:事件分类, </br>
	 *         JOB_TYPE_CODE:事件分类代码, </br>
	 *         JOB_VALIDATION: 事件分类有效性, </br>
	 *         JOB_VALIDATION_DESC: 事件分类有效性, </br>
	 *         JOB_TASK_TOTALNUM: 计划任务总数, </br>
	 *         JOB_TASK_COMPLETENUM: 完成任务数, </br>
	 *         JOB_TASK_TIMELYNUM: 及时完成任务数, </br>
	 *         JOB_TASK_PASSNUM:合格任务数 ,</br>
	 * 
	 *         }
	 */
	public List<Map<String, String>> getDailyInspectionTypeValidationList(Map<String, String> params) {
		if (params == null)
			return null;

		List<RowBase> typeList = new ArrayList<>();
		for(RowBase rb:domainService.getDomain("日常巡检分类", "1")){
			if(params.containsKey("INSPECTION_TYPE")){
				String param_id = params.get("INSPECTION_TYPE");
				if((rb.getSafeString("S_DOMAINCODE").equals(param_id))){
					typeList.add(rb);
				}
			}else{
				typeList.add(rb);
			}
		}
		if (typeList == null || typeList.size() < 1)
			return null;

		Map<String, String> typeCodeFldMap = new HashMap<String, String>(); // 分类和字段对应关系映射
		typeCodeFldMap.put("01", "A"); // "站房、辅助设备"
		typeCodeFldMap.put("02", "B"); // "采水、排水及内部管路"
		typeCodeFldMap.put("03", "C"); // "在线自动分析仪"
		typeCodeFldMap.put("04", "D"); // "电路、仪器传输"

		List<RowBase> dataList = dailyInspectionService.getDailyInspectionDataList(params);
		if (dataList == null || dataList.size() < 1)
			return null;

		int nDATA_NUM = 0;
		int nDATA_PASSNUM = 0;

		List<Map<String, String>> jobValidationList = new ArrayList<Map<String, String>>();
		for (int i = 0; i < typeList.size(); i++) {
			String strType = typeList.get(i).getSafeString("S_DOMAINCAPTION");
			String strCode = typeList.get(i).getSafeString("S_DOMAINCODE");
			String strFldPrefix = typeCodeFldMap.get(strCode);

			List<Map<String, String>> taskValidationList = new ArrayList<Map<String, String>>();
			int nComplete = 0; // 完成任务数
			int nTimely = 0; // 及时完成任务数
			int nPass = 0; // 通过任务数
			int nTotal = 0; // 总任务数
			boolean isJobValidation = true; // 事件有效性(只考虑指定巡检分类)

			nTotal = dataList.size();
			for (int j = 0; j < dataList.size(); j++) {

				nDATA_NUM = 0;
				nDATA_PASSNUM = 0;
				// 计算检查项通过的数量
				RowBase row = dataList.get(j);
				if (row == null)
					continue;

				String[] cols = row.getColumns();
				for (int nCol = 0; nCol < cols.length; nCol++) {
					if (cols[nCol].length() > 3)
						continue;

					if (cols[nCol].contains(strFldPrefix) == false) // 分类字段过滤
						continue;

					nDATA_NUM++;
					nDATA_PASSNUM += row.getSafeString(cols[nCol]).equals("1") ? 1 : 0; // dif
				}

				Map<String, String> task = cloneHashMap(params);
				// 任务信息(通用)
				task.put("TASK_CODE", dataList.get(i).getSafeString("TASK_CODE"));
				task.put("TASK_STATUS", dataList.get(i).getSafeString("TASK_STATUS"));
				task.put("TASK_STATUS_DESC", dataList.get(i).getSafeString("TASK_STATUS_DESC"));
				task.put("TASK_START_TIME", dataList.get(i).getSafeString("TASK_START_TIME"));
				task.put("TASK_END_TIME", dataList.get(i).getSafeString("TASK_END_TIME"));
				task.put("TASK_FINISH_TIME", dataList.get(i).getSafeString("TASK_FINISH_TIME"));
				task.put("GROUP_NAME", dataList.get(i).getSafeString("GROUP_NAME"));
				task.put("USER_NAME", dataList.get(i).getSafeString("USER_NAME"));
				task.put("DATA_NUM", Integer.toString(nDATA_NUM)); // 检查项总数
				task.put("DATA_PASSNUM", Integer.toString(nDATA_PASSNUM)); // dif
				task.put("TASK_PASS", nDATA_PASSNUM == nDATA_NUM ? "1" : "0"); // dif
				task.put("TASK_PASS_DESC", nDATA_PASSNUM == nDATA_NUM ? "通过" : "不通过"); // dif
				task.put("TASK_TIMELY", dataList.get(i).getSafeString("TASK_STATUS").equals("1") ? "1" : "0");
				task.put("TASK_TIMELY_DESC", dataList.get(i).getSafeString("TASK_STATUS").equals("1") ? "及时" : "不及时");
				taskValidationList.add(task);

				String strTASK_STATUS = task.get("TASK_STATUS");
				String strTASK_PASS = task.get("TASK_PASS");
				if (strTASK_STATUS.equals("1") || strTASK_STATUS.equals("3")) // 按时完成+超时完成
					nComplete++;

				if (strTASK_STATUS.equals("1")) // 按时完成
					nTimely++;

				if (strTASK_PASS.equals("1")) // 合格
					nPass++;
			}

			// 有效性判断
			if ((nPass /(double)nTotal >= 0.75) && (nTimely /(double)nTotal >= 0.75) && (nComplete /(double)nTotal >= 0.9)) {
				isJobValidation = true;
			} else {
				isJobValidation = false;
			}

			Map<String, String> jobValidation = cloneHashMap(params);
			jobValidation.put("JOB_TASK_TOTALNUM", Integer.toString(nTotal));
			jobValidation.put("JOB_TASK_COMPLETENUM", Integer.toString(nComplete));
			jobValidation.put("JOB_TASK_TIMELYNUM", Integer.toString(nTimely));
			jobValidation.put("JOB_TASK_PASSNUM", Integer.toString(nPass));
			jobValidation.put("JOB_VALIDATION", isJobValidation ? "1" : "0");
			jobValidation.put("JOB_VALIDATION_DESC", isJobValidation ? "有效" : "无效");
			jobValidation.put("JOB_TYPE", strType);
			jobValidation.put("JOB_TYPE_CODE", strCode);
			jobValidationList.add(jobValidation);
		}

		if (jobValidationList.size() < 1)
			return null;
		return jobValidationList;
	}

	/****************************************** 定期维护 **********************************************/
	/**
	 * 获取定期维护事件有效性
	 * 
	 * @param params
	 * @return
	 */
	public Map<String, String> getPeriodicalMaintainanceJobValidation(Map<String, String> params) {
		if (params == null)
			return null;

		List<Map<String, String>> taskValidationList = getPeriodicalMaintainanceTaskValidationList(params);
		if (taskValidationList == null || taskValidationList.size() < 1)
			return null;

		int nTotal = 0; // 总任务数
		int nComplete = 0; // 已完成任务数
		int nTimely = 0; // 及时完成任务数
		int nPass = 0; // 合格任务数
		boolean isJobValidation = true;

		nTotal = taskValidationList.size();
		for (int i = 0; i < taskValidationList.size(); i++) {
			Map<String, String> taskValidation = taskValidationList.get(i);
			String strTASK_STATUS = taskValidation.get("TASK_STATUS");
			String strTASK_PASS = taskValidation.get("TASK_PASS");

			if (strTASK_STATUS.equals("1") || strTASK_STATUS.equals("3")) // 按时完成+超时完成
				nComplete++;

			if (strTASK_STATUS.equals("1")) // 按时完成
				nTimely++;

			if (strTASK_PASS.equals("1")) // 合格
				nPass++;
		}

		// 有效性判断
		if ((nPass /(double)nTotal >= 0.75) && (nTimely /(double)nTotal >= 0.75) && (nComplete /(double)nTotal >= 0.9)) {
			isJobValidation = true;
		} else {
			isJobValidation = false;
		}

		Map<String, String> jobValidation = cloneHashMap(params);
		jobValidation.put("JOB_TASK_TOTALNUM", Integer.toString(nTotal));
		jobValidation.put("JOB_TASK_COMPLETENUM", Integer.toString(nComplete));
		jobValidation.put("JOB_TASK_TIMELYNUM", Integer.toString(nTimely));
		jobValidation.put("JOB_TASK_PASSNUM", Integer.toString(nPass));
		jobValidation.put("JOB_VALIDATION", isJobValidation ? "1" : "0");
		jobValidation.put("JOB_VALIDATION_DESC", isJobValidation ? "有效" : "无效");
		return jobValidation;
	}

	/**
	 * 获取定期维护事件执行的任务有效性列表
	 * 
	 * @param resqMap
	 * @return
	 */
	private List<Map<String, String>> getPeriodicalMaintainanceTaskValidationList(Map<String, String> resqMap) {
		if (resqMap == null)
			return null;

		List<RowBase> dataList = peroidMaintainanceService.getPeriodicalMaintainanceDataList(resqMap);
		if (dataList == null || dataList.size() < 1)
			return null;

		int nDATA_NUM = 0;
		int nDATA_PASSNUM = 0;

		List<Map<String, String>> taskValidationList = new ArrayList<Map<String, String>>();
		for (int i = 0; i < dataList.size(); i++) {
			nDATA_NUM = 0;
			nDATA_PASSNUM = 0;

			// 计算检查项通过的数量
			RowBase row = dataList.get(i);
			String[] cols = row.getColumns();
			for (int j = 0; j < cols.length; j++) {
				if (cols[j].length() > 3)
					continue;
				nDATA_NUM++;
				nDATA_PASSNUM += row.getSafeString(cols[j]).equals("1") ? 1 : 0; // dif
			}

			Map<String, String> task = cloneHashMap(resqMap);
			// 任务信息(通用)
			task.put("TASK_CODE", dataList.get(i).getSafeString("TASK_CODE"));
			task.put("TASK_STATUS", dataList.get(i).getSafeString("TASK_STATUS"));
			task.put("TASK_STATUS_DESC", dataList.get(i).getSafeString("TASK_STATUS_DESC"));
			task.put("TASK_START_TIME", dataList.get(i).getSafeString("TASK_START_TIME"));
			task.put("TASK_END_TIME", dataList.get(i).getSafeString("TASK_END_TIME"));
			task.put("TASK_FINISH_TIME", dataList.get(i).getSafeString("TASK_FINISH_TIME"));
			task.put("GROUP_NAME", dataList.get(i).getSafeString("GROUP_NAME"));
			task.put("USER_NAME", dataList.get(i).getSafeString("USER_NAME"));
			task.put("DATA_NUM", Integer.toString(nDATA_NUM)); // 检查项总数
			task.put("DATA_PASSNUM", Integer.toString(nDATA_PASSNUM)); // dif
			task.put("TASK_PASS", nDATA_PASSNUM == nDATA_NUM ? "1" : "0"); // dif
			task.put("TASK_PASS_DESC", nDATA_PASSNUM == nDATA_NUM ? "通过" : "不通过"); // dif
			task.put("TASK_TIMELY", dataList.get(i).getSafeString("TASK_STATUS").equals("1") ? "1" : "0");
			task.put("TASK_TIMELY_DESC", dataList.get(i).getSafeString("TASK_STATUS").equals("1") ? "及时" : "不及时");

			taskValidationList.add(task);
		}

		return taskValidationList;
	}

	
	/****************************************** 数据传输有效率 **********************************************/

	/**
	 * 获取数据传输有效率
	 * 
	 * @param params
	 *            { STATION_ID:站点编号, MN:数采仪编号 , START_TIME:考核时段开始,
	 *            END_TIME:考核时段结束 }
	 * @return { REAL_DATANUM:实收数据个数, RECEIVABLE_DATANUM:应收数据个数
	 *         REALVALIDATE_PACKETNUM:实收有效数据组数
	 *         RECEIVABLEVALIDATE_PACKETNUM:应收有效数据组数 TRANSFER_RATE:传输率
	 *         VALIDATE_RATE:有效率 TRANSFER_VALIDATE_RATE:传输有效率 }
	 */
	public Map<String, String> getDataTransferValidation(Map<String, String> params) throws Exception {
		
		// 数据传输有效率 Z= C * P *100%
		
		//实收数据个数=小时数据个数+日数据个数
		long nRealDataNum = onlineMonitorService.getRealDataNum(params)
				+onlineMonitorService.getRealDataNum_Day(params);
		
		beforeStatistic(params);//计算有效数据组前，对数据的运维有效性进行标识
		long nRealValidatePacketNum = onlineMonitorService.getRealValidatePacketNum(params);
		
		long hours = getHoursBetween(params);
		long days = hours/24;
		long nReceivableDataNum = 3*(hours/2+days); //两小时上传一次，一次三条记录，所以除以2，乘以3
		long nReceivableValidatePacketNum = hours/2;

		float dTransferRate = (float) nRealDataNum / nReceivableDataNum;
		float dValidateRate = (float) nRealValidatePacketNum / nReceivableValidatePacketNum;
		float dTransferValidationRate = dTransferRate * dValidateRate;

		Map<String, String> rateMap = new HashMap<String, String>();
		rateMap.put("REAL_DATANUM", Long.toString(nRealDataNum));
		rateMap.put("RECEIVABLE_DATANUM", Long.toString(nReceivableDataNum));
		rateMap.put("REALVALIDATE_PACKETNUM", Long.toString(nRealValidatePacketNum));
		rateMap.put("RECEIVABLEVALIDATE_PACKETNUM", Long.toString(nReceivableValidatePacketNum));
		rateMap.put("TRANSFER_RATE", Float.toString(dTransferRate));
		rateMap.put("VALIDATE_RATE", Float.toString(dValidateRate));
		rateMap.put("TRANSFER_VALIDATE_RATE", Float.toString(dTransferValidationRate));

		return rateMap;
	}
	
	/**
	 * 在统计传输有效率前，执行的操作，如果某周期内日常巡检和水样比对无效，则在该周期内的传输数据也是无效的
	 * @param params
	 */
	private void beforeStatistic(Map<String,String> params){
		
		Date start = new Date();
		
		Map<String,String> paramsCopy = cloneHashMap(params);
		paramsCopy.put("JOB_ID", "fa3ecf1a-4820-4f9f-b7b7-2553584ee7ba");
		List<Map<String,String>> list =	getDailyInspectionTaskValidationList(paramsCopy);
		if(list==null){
			return;
		}
		paramsCopy.put("JOB_ID", "0f9faa25-89e4-4ad6-869f-aa1beb70f7bc");
		List<Map<String,String>> jobParams = this.getParamJobList(paramsCopy);
		for(Map<String,String> jobParam:jobParams){
			jobParam.putAll(paramsCopy);
			List<Map<String,String>> list2 = getSampleComparisonTaskValidationList(jobParam);
			list.addAll(list2);
		}
		
		Map<String, String> mnInfo = onlineMonitorService.getStationDataCollectMN(params.get("STATION_ID"));
		if (mnInfo == null)
			return;

		String MN = mnInfo.get("MN");
		
		for (int i = 0; i < list.size(); i++) {
			Map<String, String> taskValidation = list.get(i);
			String strTASK_PASS = taskValidation.get("TASK_PASS");

			if (!strTASK_PASS.equals("1")){ // 不合格
				String startTime = taskValidation.get("TASK_START_TIME").substring(0,10);
				String endTime = taskValidation.get("TASK_END_TIME").substring(0, 10);
				updateHourDataFlag(startTime,endTime,MN,taskValidation.get("PARAM_ID"));
			}
		}
		
		Date end = new Date();
		double seconds = ((end.getTime()-start.getTime())/1000.0d);
		LOGGER.info("修改数据的运维有效性耗时： {} 秒",seconds);
	}
	
	/**
	 * 把小时数据中的 运维有效性标识OP_FLAG字段更新 为1（异常）  ，如果已经为异常了，则不更新
	 * @param startTime 开始日期
	 * @param endTime 结束日期
	 * @param mn 设备唯一标识
	 */
	private void updateHourDataFlag(String startTime,String endTime,String mn,String paramname){
		String sql = "update monitor_data_hour t set t.op_flag=1 where t.MN=@MN"
				+ " AND t.datatime >= to_date('" + startTime + " 00:00:00', 'yyyy-mm-dd hh24:mi:ss') \n"
				+ " AND t.datatime <= to_date('" + endTime + " 23:59:59', 'yyyy-mm-dd hh24:mi:ss') \n"
				+ " and (t.op_flag='0' or t.op_flag is null)";
		
		try{
			List<GlobalParameter> listParam = new ArrayList<>();
			listParam.add(new GlobalParameter("MN",mn));
			if(StringUtil.IsNotNullOrEmpty(paramname)){
				sql+=" and t.parmname=@paramname";
				listParam.add(new GlobalParameter("paramname",paramname));
			}
			DataUtil.ExecuteUpdateSqlInTransaction(sql, listParam);
		}catch(Exception e){
			LOGGER.error("updateHourDataFlag Error!",e);
		}
	}

	/**
	 * 获取一段时间的小时数
	 * @param params
	 * @return
	 * @throws ParseException
	 */
	private long getHoursBetween(Map<String, String> params) throws ParseException{
		String strSTART_TIME = params.get("START_TIME");
		String strEND_TIME = params.get("END_TIME");
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		Calendar c = Calendar.getInstance();
		Date dateStart = sdf.parse(strSTART_TIME);
		Date dateEnd = sdf.parse(strEND_TIME);
		
		//结束日期加1
		c.setTime(dateEnd);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);
		dateEnd = c.getTime();

		long hours = (dateEnd.getTime() - dateStart.getTime()) / 3600000;// 小时数
		return hours;
	}
	
	/**
	 * 获取站点下所有设备的故障记录
	 * 
	 * @param params
	 * @return { DEVICE_NUM:设备编号, DEVICE_NAME:设备名称,
	 *         DEVICE_NORMAL_TIME:设备正常运转小时数（累积）,
	 *         DEVICE_FAILURE_TIME:设备故障停运小时数（累积）, DEVICE_FAILURE_NUM:设备故障次数,
	 *         DEVICE_OPRERATE_RATE:设备运转率, DEVICE_MTBS:设备平均无故障时间,
	 *         STATION_OPERATE_TIME: 站点运转小时数}
	 */
	public List<Map<String, String>> getStationDeviceOperateRateList(Map<String, String> params)
			throws Exception {

		List<Map<String, String>> deviceList = onlineMonitorService.getStationDeviceList(params);
		if (deviceList == null || deviceList.size() < 1)
			return null;

		List<RowBase> failureList = failureRepairService.getStationDeviceFailureRcdList(params);
		List<Map<String, String>> deviceOprRateList = new ArrayList<Map<String, String>>();

		long nStationOperateTime = 0;
		long nDeviceNormalTime = 0;
		long nDeviceFailureTime = 0;
		long nDeviceFailureNum = 0;
		long nDeviceMTBS = 0;
		float fDeviceOperateRate = 0;

		nStationOperateTime = getStationOperateHours(params); // 站点运行小时数

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

		for (int i = 0; i < deviceList.size(); i++) {

			nDeviceFailureTime = 0;
			nDeviceNormalTime = 0;
			nDeviceFailureNum = 0;
			nDeviceMTBS = 0;

			Map<String, String> deviceOprRate = cloneHashMap(params);
			deviceOprRate.put("DEVICE_NUM", deviceList.get(i).get("DEVICE_NUM"));
			deviceOprRate.put("DEVICE_NAME", deviceList.get(i).get("DEVICE_NAME"));

			if (failureList == null || failureList.size() < 1) {
				nDeviceFailureTime = nStationOperateTime;
				nDeviceNormalTime = nStationOperateTime - nDeviceFailureTime;
				nDeviceFailureNum = 0;
				nDeviceMTBS = nDeviceNormalTime;
				fDeviceOperateRate = 1; // nDeviceNormalTime/nStationOperateTime;
			} else {

				for (int j = 0; j < failureList.size(); j++) {
					if (deviceList.get(i).get("DEVICE_NUM").equals(failureList.get(j).getSafeString("DEVICE_NUMBER"))) {
						nDeviceFailureNum++;

						String strSTART_TIME = failureList.get(j).getSafeString("BEGIN_TIME");
						String strEND_TIME = failureList.get(j).getSafeString("END_TIME");

						if (strEND_TIME.length() < 1) {
							SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
							strEND_TIME = df.format(new Date());
						}

						Date dateStart = sdf.parse(strSTART_TIME);
						Date dateEnd = sdf.parse(strEND_TIME);

						long hours = (dateEnd.getTime() - dateStart.getTime()) / 1000 / 60 / 60;// 设备故障小时数
						nDeviceFailureTime += hours;
					}
				}

				nDeviceNormalTime = nStationOperateTime - nDeviceFailureTime;
				nDeviceMTBS = nDeviceFailureNum > 0 ? nDeviceNormalTime / nDeviceFailureNum : nDeviceNormalTime;
				fDeviceOperateRate = (float) nDeviceNormalTime / nStationOperateTime;
			}

			deviceOprRate.put("DEVICE_NORMAL_TIME", Long.toString(nDeviceNormalTime));
			deviceOprRate.put("DEVICE_FAILURE_TIME", Long.toString(nDeviceFailureTime));
			deviceOprRate.put("DEVICE_FAILURE_NUM", Long.toString(nDeviceFailureNum));
			deviceOprRate.put("DEVICE_OPRERATE_RATE", Float.toString(fDeviceOperateRate));
			deviceOprRate.put("DEVICE_MTBS", Long.toString(nDeviceMTBS));
			deviceOprRate.put("STATION_OPERATE_TIME", Long.toString(nStationOperateTime));

			deviceOprRateList.add(deviceOprRate);
		}

		if (deviceOprRateList.size() < 1)
			return null;

		return deviceOprRateList;
	}

	/**
	 * 获取数采仪在线率
	 * 
	 * @param resqMap
	 * @return {OFFLINE_NUM:掉线次数, OFFLINE_TIME:累积掉线小时数, ONLINE_TIME:累积在线小时数,
	 *         STATION_OPRERATE_TIME:站点运行时间, ONLINE_RATE:在线率}
	 * @throws Exception
	 */
	public Map<String, String> getStationMNOnlineRate(Map<String, String> resqMap) throws Exception {

		Map<String, String> mnInfo = onlineMonitorService.getStationDataCollectMN(resqMap.get("STATION_ID"));
		if (mnInfo == null)
			return null;

		long nOfflineNum = 0;
		long nOfflineTime = 0;
		long nOnlineTime = 0;
		long nStationOperateTime = 0;
		float fMNOnlineRate = 0;

		resqMap.put("MN", mnInfo.get("MN"));

		nStationOperateTime = getStationOperateHours(resqMap); // 站点运行小时数
		List<RowBase> offlineList = onlineMonitorService.getMNOfflineRcdList(resqMap);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

		if (offlineList == null || offlineList.size() < 1) {
			nOfflineNum = 0;
			nOfflineTime = 0;
			nOnlineTime = nStationOperateTime - nOfflineTime;
			fMNOnlineRate = 1; //
		} else {

			for (int i = 0; i < offlineList.size(); i++) {
				nOfflineNum++;

				String strSTART_TIME = offlineList.get(i).getSafeString("OFFLINE_TIME");
				String strEND_TIME = offlineList.get(i).getSafeString("ONLINE_TIME");

				if (strEND_TIME.length() < 1) {
					SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					strEND_TIME = df.format(new Date()); // 取当前时间
				}

				Date dateStart = sdf.parse(strSTART_TIME);
				Date dateEnd = sdf.parse(strEND_TIME);

				long hours = (dateEnd.getTime() - dateStart.getTime()) / 1000 / 60 / 60;// 数采仪掉线小时数
				nOfflineTime += hours;
			}
			nOnlineTime = nStationOperateTime - nOfflineTime;
			fMNOnlineRate = (float) nOnlineTime / nStationOperateTime;
		}

		Map<String, String> mnOnlineRate = cloneHashMap(resqMap);
		mnOnlineRate.put("OFFLINE_NUM", Long.toString(nOfflineNum));
		mnOnlineRate.put("OFFLINE_TIME", Long.toString(nOfflineTime));
		mnOnlineRate.put("ONLINE_TIME", Long.toString(nOnlineTime));
		mnOnlineRate.put("STATION_OPRERATE_TIME", Long.toString(nStationOperateTime));
		mnOnlineRate.put("ONLINE_RATE", Float.toString(fMNOnlineRate));
		return mnOnlineRate;
	}

	/**
	 * 获取考核时间段内站点运转小时数（理论值）
	 * 
	 * @param resqMap
	 * @return
	 */
	private long getStationOperateHours(Map<String, String> resqMap) throws Exception {
		if (resqMap == null)
			return 0;

		String strSTART_TIME = resqMap.get("START_TIME");
		String strEND_TIME = resqMap.get("END_TIME");

		// 获取运行时间：（M-N）=考核时段小时数 -考核时段停产（排）期小时， 现在默认考核期间24小时全天候运行
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		Date dateStart = sdf.parse(strSTART_TIME);
		Date dateEnd = sdf.parse(strEND_TIME);
		c.setTime(dateEnd);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);
		dateEnd = c.getTime();

		// 获取停运事件：查询站点设备停运记录
		long hours = (dateEnd.getTime() - dateStart.getTime()) / 1000 / 60 / 60;// 小时数
		return hours;
	}
	
	/**
	 * 拷贝HashMap
	 * 
	 * @param resqMap
	 * @return
	 */
	private Map<String, String> cloneHashMap(Map<String, String> resqMap) {
		if (resqMap == null)
			return null;
		HashMap<String, String> rtnMap = new HashMap<String, String>();
		Set<Entry<String, String>> sets = resqMap.entrySet();
		for (Entry<String, String> entry : sets) {
			rtnMap.put(entry.getKey(), entry.getValue());
		}

		return rtnMap;
	}
}
